package com.bigdata;


import com.bigdata.utils.PropertiesUtil;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;

public class KafkaHbaseConsumer {


    public static void main(String[] args) throws Exception {

        /*Properties props = new Properties();

        // 定义kakfa 服务的地址，不需要将所有broker指定上
        props.put("bootstrap.servers", "192.168.136.128:9092");
        // 制定consumer group
        props.put("group.id", "kafka_hbase");
        // 是否自动确认offset
        props.put("enable.auto.commit", "true");
        // 自动确认offset的时间间隔
        props.put("auto.commit.interval.ms", "1000");
        // key的序列化类
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        // value的序列化类
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");*/
        // 定义consumer
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(PropertiesUtil.properties);

        // 消费者订阅的topic, 可同时订阅多个
        consumer.subscribe(Arrays.asList(PropertiesUtil.getProperty("kafka.topics")));

        HBaseDao hbaseDao = new HBaseDao();
        while (true) {
            // 读取数据，读取超时时间为100ms
            ConsumerRecords<String, String> records = consumer.poll(100);

            for (ConsumerRecord<String, String> record : records) {

                //System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
                //获取到kafka 集群中的数据后，编写代码到hbase表中

                System.out.println("1111");
                hbaseDao.put(record.value());

            }

        }
    }
}
